package com.ticket.sass.admin.dto;

import com.alibaba.fastjson.JSONObject;
import com.ticket.sass.common.entity.tenant.ResourceEntity;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.jetbrains.annotations.NotNull;

import java.util.List;

/**
 * @author ywb
 * @date 2022-01-07 16:21
 */
@Data
@ApiModel(value = "递归资源Out")
public class PermResourceOutput implements Comparable<PermResourceOutput> {
    @ApiModelProperty("资源ID")
    private Integer id;
    
    private String name;
    
    @ApiModelProperty(value = "资源等级")
    private Integer level;
    
    @ApiModelProperty("资源父ID")
    private Integer parentId;
    
    @ApiModelProperty("排序")
    private Integer sort;

    @ApiModelProperty(value = "权限标识符")
    private String perms;
    
    @ApiModelProperty(value = "备注")
    private String remark;
    
    @ApiModelProperty(value = "路径")
    private String path;
    
    @ApiModelProperty(value = "图表")
    private String iconSvg;
    
    @ApiModelProperty(value = "是否隐藏菜单")
    private Boolean hideInMenu;
    
    @ApiModelProperty(value = "路由名称")
    private String routerName;
    @ApiModelProperty("菜单资源")
    private List<PermResourceOutput> children;
    
    @Override
    public int compareTo(@NotNull PermResourceOutput o) {
        return this.sort.compareTo(o.getSort());
    }
    
    // 构造方法
    public PermResourceOutput(ResourceEntity entity) {
        this.id = entity.getId();
        this.name = entity.getName();
        this.level = entity.getLevel();
        this.parentId = entity.getParentId();
        this.sort = entity.getSort();
        this.perms = entity.getPerms();
        this.remark = entity.getRemark();
        
        // 解析 data JSON 字符串
        if (entity.getData() != null && !entity.getData().isEmpty()) {
            JSONObject dataJson = JSONObject.parseObject(entity.getData());
            this.path = dataJson.getString("path");
            this.iconSvg = dataJson.getString("iconSvg");
            this.hideInMenu = dataJson.getBoolean("hideInMenu");
            this.routerName = dataJson.getString("routerName");
        }
    }
}
